package org.example.config;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;
import org.example.store.MongoChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.w3c.dom.Text;

@Configuration
public class XiaozhiAgentConfig {
    @Autowired
    private MongoChatMemoryStore chatMemoryStore;

    @Autowired
    private EmbeddingModel embeddingModel;
    @Bean
    public ChatMemoryProvider chatMemoryProviderXiaozhi() {
        return memoryId-> MessageWindowChatMemory.
                builder()
                .id(memoryId)
                .chatMemoryStore(chatMemoryStore)
                .maxMessages(20).build();
    }

    @Bean
    public ContentRetriever contentRetrieverXiaozhi() {
        Document document=FileSystemDocumentLoader.loadDocument("C:\\Users\\ADMIN\\Desktop\\AI\\agent\\PowerForm.md");
        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore();
        EmbeddingStoreIngestor.ingest(document, embeddingStore);
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }


    @Bean
    public EmbeddingStore<TextSegment> embeddingStore() {
        MilvusEmbeddingStore embeddingStore = MilvusEmbeddingStore.builder()
                .host("localhost")      // Docker宿主机IP
                .port(19530)            // 默认端口
                .collectionName("powerform")
                .dimension(embeddingModel.dimension())         // 向量维度需与嵌入模型匹配
                .build();
        return embeddingStore;
    }


}
